package org.geektimes.projects.user.oauth2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
* 认证委托器  
* @author : KangNing Hu
*/
public class OAuth2AuthenticationDelegate implements OAuth2Authentication{


	// 认证器列表
	private List<OAuth2Authentication> oAuth2Authentications = new ArrayList<>();


	/**
	 * 添加认证器
	 * @param auth2Authentication 认证器
	 */
	public void addAuthentication(OAuth2Authentication auth2Authentication){
		this.oAuth2Authentications.add(auth2Authentication);
	}


	@Override
	public boolean support(OAuth2Configuration configuration) {
		return true;
	}

	@Override
	public Oauth2Token authenticated(OAuth2Configuration configuration, HttpServletRequest request, HttpServletResponse response) {
		for (OAuth2Authentication oAuth2Authentication : oAuth2Authentications){
			if (oAuth2Authentication.support(configuration)){
				return oAuth2Authentication.authenticated(configuration , request , response);
			}
		}
		return new Oauth2Token();
	}

}
